<?php

class Youtube {

    public function getMp4($video_link) {
        $id = explode("?",$video_link);
        $id = str_replace("v=","", $id[1]);
        $API_URL = "http://www.youtube.com/get_video_info?&video_id=$id";
        $video_info = file_get_contents($API_URL);

        $url_encoded_fmt_stream_map = '';
        parse_str($video_info);

        if (isset($url_encoded_fmt_stream_map)) {
            $my_formats_array = explode(',', $url_encoded_fmt_stream_map);
        } else {
            return '201';
            //return 'Nh?p video ID dúng di b?n';
        }
        if (count($my_formats_array) == 0) {
            return '201';
            //return 'Nh?p video ID dúng di b?n';
        }

        $avail_formats[] = '';
        $i = 0;
        $ipbits = $ip = $itag = $sig = $quality = $type = $url = '';
        $expire = time();

        foreach ($my_formats_array as $format) {
            parse_str($format);

            $avail_formats[$i]['itag'] = $itag;
            $avail_formats[$i]['quality'] = $quality;
            $type = explode(';', $type);
            $avail_formats[$i]['type'] = $type[0];
            $avail_formats[$i]['url'] = urldecode($url) . '&signature=' . $sig;
            parse_str(urldecode($url));
            $avail_formats[$i]['expires'] = date("G:i:s T", $expire);
            $avail_formats[$i]['ipbits'] = $ipbits;
            $avail_formats[$i]['ip'] = $ip;
            $i++;
        }
        return $avail_formats;
    }

}
